package club.justwill.single.Controller;

import club.justwill.single.Service.ModelService;
import club.justwill.single.assembler.ModelAssemble;
import club.justwill.single.bean.SearchModels;
import club.justwill.single.model.CoreConstants;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.persistence.jpo.Model;
import club.justwill.single.util.JsonResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by luoyu on 1/5/16.
 */
@RestController
@Scope(WebApplicationContext.SCOPE_SESSION)
public class ModelController extends BaseController {

    @Autowired
    private ModelService modelService;

    @Autowired
    private ModelAssemble modelAssemble;

    @RequestMapping(value = URL_MODEL_PROJECT_GET, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse searchModelsandProjects (@RequestBody SearchModels smap) throws IOException {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        List<Model> modelList = modelService.searchModelsandProjects(smap, obj);
        if (JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        }else {
            obj.setData(modelAssemble.assembleModels(modelList));
            obj.setSuccessful();
        }
        return obj;
    }
}
